package org.qf.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author young
 * @create 2021-01-11-11:03
 * @description
 */
public class BaseDao {
    private Connection connection;

    private PreparedStatement preparedStatement;

    private ResultSet resultSet;

    private static String driver;

    private static String url;

    private static String username;

    private static String password;

    //静态代码块
    static {
        initial();
    }

    //读取数据库配置文件
    public static void initial(){
        Properties properties=new Properties();
        InputStream inputStream=BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver=properties.getProperty("driver");
        url=properties.getProperty("url");
        username=properties.getProperty("username");
        password=properties.getProperty("password");

    }



    //创建连接的方法
    public boolean getConnection(){
        try {
            // alter +enter
            Class.forName(driver);
            connection= DriverManager.getConnection(url,username,password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return  false;
        }
        return true;
    }


    //关闭连接
    public void closeAll(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(preparedStatement!=null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    // 增删改
    public int executeUpdate(String sql,Object[] params){
        int count=0;
        if(getConnection()){
            try {
                preparedStatement=connection.prepareStatement(sql);
                for (int i=0;i<params.length;i++){
                    preparedStatement.setObject(i+1,params[i]);
                }
                count=preparedStatement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return count;
    }


    //查询
    public ResultSet executeSQL(String sql,Object[] params){
        if(this.getConnection()){
            try {
                preparedStatement=connection.prepareStatement(sql);
                for (int i=0;i<params.length;i++){
                    preparedStatement.setObject(i+1,params[i]);
                }
                resultSet=preparedStatement.executeQuery();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return resultSet;
    }



}
